<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import GoodsEditBase from "components/GoodsEditBase/index";
import GoodsEditDeliver from "components/GoodsEditDeliver/index";
import GoodsEditSpecs from "components/GoodsEditSpecs/index";
import GoodsEditGroup from "components/GoodsEditGroup/index";
import GoodsEditAttr from "components/GoodsEditAttr/index";
import GoodsEditParam from "components/GoodsEditParam/index";
import GoodsEditServe from "components/GoodsEditServe/index";
import GoodsEditOther from "components/GoodsEditOther/index";
import GoodsEditHtml from "components/GoodsEditHtml/index";
export default {
    mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
    components: {
        GoodsEditBase,
        GoodsEditDeliver,
        GoodsEditSpecs,
        GoodsEditGroup,
        GoodsEditAttr,
        GoodsEditParam,
        GoodsEditServe,
        GoodsEditOther,
        GoodsEditHtml,
    },
    data() {
        return {
            appWindow: window,
            // 步骤
            stepIndex: 0,

            tabs: [
                { title: "基础", icon: "el-icon-coin", value: 0 },
                { title: "配送", icon: "el-icon-map-location", value: 1 },
                { title: "规格", icon: "el-icon-cpu", value: 2 },
                { title: "属性", icon: "el-icon-postcard", value: 3 },
                { title: "其他", icon: "el-icon-receiving", value: 4 },
                { title: "内容", icon: "el-icon-news", value: 5 },
            ],
            editData: {
                // 基础
                base: null,
                // 配送
                deliver: null,
                // 规格
                specs: null,
                // 分组
                group: null,
                // 属性
                attr: null,
                // 参数
                param: null,
                // 服务
                serve: null,
                // 其他
                other: null,
                // 富文本
                html: null,
            },
            form: {},
            // 统一规格值
            skuObj: {},
            // 页面接口地址
            apiUrl: {
                create: "mall-site-portal/product/create",
                update: "mall-site-portal/product/update",
                detail: "mall-site-portal/product/getItem",
            },
            // 表格查询数据
            mixinTable_tableSearchData: {
                // 父参数
                // dictType: "",
            },
            // 表格数据
            tableData: [],
            loading: true,
            detailInfo: null,
            // 配送模板信息
            templateOptions: [],
            load: {
                all: false,
                // 基础
                base: false,
                // 配送
                deliver: false,
                // 规格
                specs: false,
                // 分组
                group: false,
                // 属性
                attr: false,
                // 参数
                param: false,
                // 服务
                serve: false,
                // 其他
                other: false,
                // 富文本
                html: false,
            },
        };
    },

    methods: {
        changeBase(e) {
            this.setChangeData("base", e);
        },
        changeDeliver(e) {
            this.setChangeData("deliver", e);
        },
        changeSpecs(e) {
            this.setChangeData("specs", e);
        },
        changeGroup(e) {
            this.setChangeData("group", e);
        },
        changeAttr(e) {
            this.setChangeData("attr", e);
        },
        changeParam(e) {
            this.setChangeData("param", e);
        },
        changeServe(e) {
            this.setChangeData("serve", e);
        },
        changeOther(e) {
            this.setChangeData("other", e);
        },
        changeHtml(e) {
            this.setChangeData("html", e);
        },
        setChangeData(type_, e) {
            this.editData[type_] = {
                ...e.data,
            };
            this.load[type_] = e.load;
            let _is = 0;
            for (let i in this.load) {
                if (!this.load[i] && i != "all") {
                    _is += 1;
                }
            }
            if (_is <= 0) {
                this.$vFN.Tools.setLoad(false);
                this.load["all"] = true;
            }
        },
        hideBox() {
            this.loading = true;
            this.mixinEdit_editVisible = false;
            setTimeout(() => {
                this.detailInfo = null;
                // this.$refs["GoodsEditBase"].uninstall();
                // this.$refs["GoodsEditDeliver"].uninstall();
                // this.$refs["GoodsEditSpecs"].uninstall();
                // this.$refs["GoodsEditGroup"].uninstall();
                // this.$refs["GoodsEditAttr"].uninstall();
                // this.$refs["GoodsEditParam"].uninstall();
                // this.$refs["GoodsEditServe"].uninstall();
                // this.$refs["GoodsEditOther"].uninstall();
                // this.$refs["GoodsEditHtml"].uninstall();
            }, 500);
        },

        // 初始化
        async init() {
            this.loading = true;
            this.mixinEdit_editVisible = true;
            // this.$vFN.Tools.setLoad(true)
            if (this.mixinEdit_editType == "add") {
                this.mixinEdit_editTitle = "商品创建";
            } else if (this.mixinEdit_editType == "update") {
                this.mixinEdit_editTitle = "商品修改";
                let _data = await this.$vFN.Http.post(
                    {
                        domain: "APP_MAIN_DOMAIN",
                        url: this.apiUrl.detail,
                        data: {
                            id: this.id,
                        },
                    },
                    window
                );

                if (_data._ok) {
                    this.detailInfo = _data.data;
                }
            } else if (this.mixinEdit_editType == "copy") {
                this.mixinEdit_editTitle = "商品复制";
                let _data = await this.$vFN.Http.post(
                    { 
                        domain: "APP_MAIN_DOMAIN",
                        url: this.apiUrl.detail, data: { id: this.id } 
                    },
                    window
                );

                if (_data._ok) {
                    this.detailInfo = _data.data;
                }
            }

            let _p = [
                await this.getTemplate(),
                await this.$refs["GoodsEditBase"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
                await this.$refs["GoodsEditDeliver"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
                await this.$refs["GoodsEditSpecs"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
                await this.$refs["GoodsEditGroup"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
                await this.$refs["GoodsEditAttr"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
                await this.$refs["GoodsEditParam"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
                await this.$refs["GoodsEditServe"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
                await this.$refs["GoodsEditOther"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
                await this.$refs["GoodsEditHtml"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo,
                }),
            ];
            Promise.all(_p).then(() => {
                this.setStep(0);
            });

            
        },
        // 获取配送模板信息
        async getTemplate() {
            this.templateOptions = await this.$vFN.Tools.getSelectListSource(
                {
                    domain: "APP_MAIN_DOMAIN",
                    url: "mall-site-portal/product/listFreight",
                    format: false,
                },
                window
            );
        },

        // 步骤
        async setStep(index_) {
            this.loading = true;
            this.stepIndex = index_;

            this.loading = false;
        },

        // 商品分类数据源

        async submit() {
            let _baseIs = await this.$refs["GoodsEditBase"].validate();
            console.log("_baseIs", _baseIs);
            if (!_baseIs) {
                this.setStep(0);
                return;
            }
            let _deliverIs = await this.$refs["GoodsEditDeliver"].validate();
            console.log("_deliverIs", _deliverIs);
            if (!_deliverIs) {
                this.setStep(1);
                return;
            }
            let _specsIs = await this.$refs["GoodsEditSpecs"].validate();
            console.log("_specsIs", _specsIs);
            if (!_specsIs) {
                this.setStep(2);
                return;
            }
            let _groupIs = await this.$refs["GoodsEditGroup"].validate();
            console.log("_groupIs", _groupIs);
            let _attrIs = await this.$refs["GoodsEditAttr"].validate();
            console.log("_attrIs", _attrIs);
            let _paramIs = await this.$refs["GoodsEditParam"].validate();
            console.log("_paramIs", _paramIs);
            let _serveIs = await this.$refs["GoodsEditServe"].validate();
            console.log("_serveIs", _serveIs);
            if (!_groupIs || !_attrIs || !_paramIs || !_serveIs) {
                this.setStep(3);
                return;
            }
            let _otherIs = await this.$refs["GoodsEditOther"].validate();
            console.log("_otherIs", _otherIs);
            if (!_otherIs) {
                this.setStep(4);
                return;
            }
            let _htmlIs = await this.$refs["GoodsEditHtml"].validate();
            console.log("_htmlIs", _htmlIs);
            if (!_htmlIs) {
                this.setStep(5);
                return;
            }

            this.loading = true;

            let _baseData = this.$refs["GoodsEditBase"].getData();
            let _deliverData = this.$refs["GoodsEditDeliver"].getData();
            let _specsData = this.$refs["GoodsEditSpecs"].getData();
            let _groupData = this.$refs["GoodsEditGroup"].getData();
            let _attrData = this.$refs["GoodsEditAttr"].getData();
            let _paramData = this.$refs["GoodsEditParam"].getData();
            let _serveData = this.$refs["GoodsEditServe"].getData();
            let _otherData = this.$refs["GoodsEditOther"].getData();
            let _htmlData = this.$refs["GoodsEditHtml"].getData();
            let _form = {
                ..._baseData,
                ..._deliverData,
                ..._specsData,
                ..._groupData,
                ..._attrData,
                ..._paramData,
                ..._serveData,
                ..._otherData,
                ..._htmlData,
            };

            if (this.mixinEdit_editType == "add") {
                let _data = await this.$vFN.Http.post(
                    {
                        domain: "APP_MAIN_DOMAIN",
                        url: this.apiUrl.create,
                        data: _form,
                    },
                    window
                );
                if (_data._ok) {
                    this.hideBox();
                    this.onSubmitSuccess();
                    this.$vFN.Tools.messageSuccess("添加成功");
                }
            } else if (this.mixinEdit_editType == "update") {
                _form.id = this.id;
                let _data = await this.$vFN.Http.post(
                    {
                        domain: "APP_MAIN_DOMAIN",
                        url: this.apiUrl.update,
                        data: _form,
                    },
                    window
                );
                if (_data._ok) {
                    this.hideBox();
                    this.onSubmitSuccess();
                    this.$vFN.Tools.messageSuccess("修改成功");
                }
            } else if (this.mixinEdit_editType == "copy") {
                let _data = await this.$vFN.Http.post(
                    {
                        domain: "APP_MAIN_DOMAIN",
                        url: this.apiUrl.create,
                        data: _form,
                    },
                    window
                );
                if (_data._ok) {
                    this.hideBox();
                    this.onSubmitSuccess();
                    this.$vFN.Tools.messageSuccess("复制成功");
                }
            }

            this.loading = false;
        },

        // 提交成功
        onSubmitSuccess() {
            this.onSuccess();
        },
        // 成功回调
        onSuccess() {},
        // 取消回调
        onCancel() {},
        // 失败回调
        onError() {},
    },
    mounted() {
        this.init();
    },
    activated() {},
};
</script>